home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / DroneZone / DZSpace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  3.6 KB  |  151 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        DZSpace.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Contents:    Submits the spacejunk.
  6.  *
  7.  *    Copyright © 1996 Apple Computer, Inc.
  8.  */
  9.  
  10. #include <assert.h>
  11.  
  12. #include <Quickdraw.h>
  13.  
  14. #include "QD3D.h"
  15. #include "QD3DGeometry.h"
  16. #include "QD3DShader.h"
  17. #include "QD3DView.h"
  18.  
  19. #include "DZSpace.h"
  20.  
  21.  
  22. #define SPACE_INTERVAL        2.0            // Grid cell size
  23. #define SPACE_CUBE            3            // Half side of the cube of cells
  24. #define SPACE_SPREAD1        0.0001        // Mulitplier to Random for line start
  25. #define SPACE_SPREAD2        0.00002        // Mulitplier to Random for line length
  26.  
  27.  
  28. static TQ3AttributeSet    gSpaceColor            = NULL;
  29.  
  30.  
  31. /* =============================================================================
  32.  *        Space_Init (external)
  33.  *
  34.  *    Initializes the space stuff.
  35.  * ========================================================================== */
  36. void Space_Init(
  37.     void)
  38. {
  39.     gSpaceColor = Q3AttributeSet_New();
  40. }
  41.  
  42.  
  43. /* =============================================================================
  44.  *        Space_Exit (external)
  45.  *
  46.  *    Prepares for exit.
  47.  * ========================================================================== */
  48. void Space_Exit(
  49.     void)
  50. {
  51.     if (gSpaceColor != NULL)
  52.     {
  53.         Q3Object_Dispose(gSpaceColor);
  54.         gSpaceColor = NULL;
  55.     }
  56. }
  57.  
  58.  
  59. /* =============================================================================
  60.  *        Space_Submit (external)
  61.  *
  62.  *    Submits all the 3D geometry of the spacejunk.
  63.  * ========================================================================== */
  64. void Space_Submit(
  65.     TQ3ViewObject        inView,
  66.     const TQ3Point3D*    inPosition,
  67.     const TQ3Vector3D*    inDirection)
  68. {
  69.     long                x;
  70.     long                y;
  71.     long                z;
  72.     long                loX;
  73.     long                loY;
  74.     long                loZ;
  75.     long                hiX;
  76.     long                hiY;
  77.     long                hiZ;
  78.     TQ3LineData            lineData;
  79.     float                px;
  80.     float                py;
  81.     float                pz;
  82.     TQ3ColorRGB            color;
  83.     
  84.     assert(inView != NULL);
  85.     assert(inPosition != NULL);
  86.     assert(inDirection != NULL);
  87.     
  88.     // Find the center grid cell
  89.     px = inPosition->x + inDirection->x*SPACE_INTERVAL*SPACE_CUBE;
  90.     py = inPosition->y + inDirection->y*SPACE_INTERVAL*SPACE_CUBE;
  91.     pz = inPosition->z + inDirection->z*SPACE_INTERVAL*SPACE_CUBE;
  92.     
  93.     x = (px + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  94.     y = (py + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  95.     z = (pz + 0.5*SPACE_INTERVAL)/SPACE_INTERVAL;
  96.     
  97.     // Find the range to cover
  98.     loX = x - SPACE_CUBE;
  99.     loY = y - SPACE_CUBE;
  100.     loZ = z - SPACE_CUBE;
  101.     
  102.     hiX = loX + 2*SPACE_CUBE;
  103.     hiY = loY + 2*SPACE_CUBE;
  104.     hiZ = loZ + 2*SPACE_CUBE;
  105.     
  106.     // Set up the line data
  107.     lineData.vertices[0].attributeSet    = NULL;
  108.     lineData.vertices[1].attributeSet    = NULL;
  109.     lineData.lineAttributeSet            = gSpaceColor;
  110.     
  111.     // Do the cube
  112.     for (x = loX; x <= hiX; x++)
  113.     {
  114.         px = (x+0.5)*SPACE_INTERVAL;
  115.         
  116.         for (y = loY; y <= hiY; y++)
  117.         {
  118.             py = (y+0.5)*SPACE_INTERVAL;
  119.             
  120.             for (z = loZ; z <= hiZ; z++)
  121.             {
  122.                 pz = (z+0.5)*SPACE_INTERVAL;
  123.                 
  124.                 // Make Random() repeatable based on cell index
  125.                 qd.randSeed = x^y^z;
  126.                 
  127.                 // Set up the endpoints
  128.                 lineData.vertices[0].point.x = px + Random()*SPACE_SPREAD1;
  129.                 lineData.vertices[0].point.y = py + Random()*SPACE_SPREAD1;
  130.                 lineData.vertices[0].point.z = pz + Random()*SPACE_SPREAD1;
  131.                 
  132.                 lineData.vertices[1].point.x = lineData.vertices[0].point.x + Random()*SPACE_SPREAD2;
  133.                 lineData.vertices[1].point.y = lineData.vertices[0].point.y + Random()*SPACE_SPREAD2;
  134.                 lineData.vertices[1].point.z = lineData.vertices[0].point.z + Random()*SPACE_SPREAD2;
  135.                 
  136.                 // Set up the line color -- blend between purple and green
  137.                 color.b = ((unsigned short) Random()) * (1.0/65535.0);
  138.                 color.r = 0.4*color.b;
  139.                 color.g = 0.8*(1.0-color.b);
  140.                 
  141.                 Q3AttributeSet_Add(gSpaceColor, kQ3AttributeTypeDiffuseColor, &color);
  142.                 
  143.                 // Submit the line
  144.                 Q3Line_Submit(&lineData, inView);
  145.             }
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151.